package com.liuqm.controller;

import com.liuqm.pojo.CzktProductClass;
import com.liuqm.service.ICzktProductClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author liuqm
 * @since 2024-09-04
 */
@RestController
@RequestMapping("/product/class")
@CrossOrigin
public class CzktProductClassController {

    @Autowired
    ICzktProductClassService czktProductClassService;


//    @Autowired
//    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询一级分类
     *
     * @return
     */
    @RequestMapping("/list")
    public Object getClassList() {
        return czktProductClassService.list(0);
    }


    /**
     * 根据父类id查询所有子类
     *
     * @param pid
     * @return
     */
    @RequestMapping("/secondList")
    public Object getChildList(Integer pid) {

        //查询二级分类
        List<CzktProductClass> secondList = czktProductClassService.list(pid);

        Set<Integer> ids = secondList.stream().map(item -> {
            return item.getClassId();
        }).collect(Collectors.toSet());
        //查询三级分类
        List<CzktProductClass> thirdList = czktProductClassService.list(ids);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("secondList", secondList);
        resultMap.put("thirdList", thirdList);

        return resultMap;
    }


}
